<?php

class PdoTypeArt extends PdoConnect {

    public function create(typeart $typeart) {
        try {
            $this->open();
            $sql = "INSERT INTO `typeart`(IDTYPEART, NOMART) VALUES(?, ?)";

            $prep = $this->pdo->prepare($sql);

            $prep->bindValue(1, $typeart->getIdTypeArt(), PDO::PARAM_INT);
            $prep->bindValue(2, $typeart->getNomArt(), PDO::PARAM_STR);



            $prep->execute();
        } catch (PDOException $e) {
            $msg = 'ERREUR PDO dans ' . $e->getFile() . ' L.' . $e->getLine() . ' : ' . $e->getMessage();
            die($msg);
        }
    }

    public function readAll() {
        try {
            $this->open();
            $sql = "SELECT * FROM `typeart` ORDER BY NOMART";
            $prep = $this->pdo->prepare($sql);

            $prep->execute();
            $prep->setFetchMode(PDO::FETCH_ASSOC);


            return $prep;
        } catch (PDOException $e) {
            $msg = 'ERREUR PDO dans ' . $e->getFile() . ' L.' . $e->getLine() . ' : ' . $e->getMessage();
            die($msg);
        }
    }

    public function readOne($IdTypeArt) {
        try {
            $this->open();
            $sql = "SELECT * FROM `typeart` WHERE IDTYPEART=$IdTypeArt";
            $prep = $this->pdo->prepare($sql);

            $prep->execute();

            $typeart = new TypeArt();
            $ligne = $prep->fetch(PDO::FETCH_OBJ);
            $typeart->setIdTypeArt($ligne->IDTYPEART);
            $typeart->setNomArt($ligne->NOMART);


            return $typeart;
        } catch (PDOException $e) {
            $msg = 'ERREUR PDO dans ' . $e->getFile() . ' L.' . $e->getLine() . ' : ' . $e->getMessage();
            die($msg);
        }
    }

    public function delete(typeart $typeart) {

        try {
            $this->open();
            $sql = "DELETE FROM `typeart` WHERE IDTYPEART=?";

            $prep = $this->pdo->prepare($sql);

            $prep->bindValue(1, $typeart->getIdTypeArt(), PDO::PARAM_INT);
            $prep->execute();
        } catch (PDOException $e) {
            $msg = 'ERREUR PDO dans ' . $e->getFile() . ' L.' . $e->getLine() . ' : ' . $e->getMessage();
            die($msg);
        }
    }

    public function update(typeart $typeart) {
        try {
            $this->open();
            $sql = "UPDATE `typeart` SET NOMART= ? WHERE IDTYPEART=?";

            $prep = $this->pdo->prepare($sql);

            $prep->bindValue(1, $typeart->getNomArt(), PDO::PARAM_STR);
            $prep->bindValue(3, $typeart->getIdTypeArt(), PDO::PARAM_INT);


            $prep->execute();
        } catch (PDOException $e) {
            $msg = 'ERREUR PDO dans ' . $e->getFile() . ' L.' . $e->getLine() . ' : ' . $e->getMessage();
            die($msg);
        }
    }

    public function exist($nomArt) {
        $this->open();
        $query = "SELECT * FROM typeart WHERE NomArt='$nomArt';";
        $statement = $this->pdo->query($query);
        return $statement->rowCount();
    }

}
